using ERPBackend.Data;
using ERPBackend.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace ERPBackend.Services
{
    public class SecondLevelCategoryService : ISecondLevelCategoryService
    {
        private readonly ApplicationDbContext _context;

        public SecondLevelCategoryService(ApplicationDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public async Task<IEnumerable<SecondLevelCategory>> GetAllSecondLevelCategoriesAsync()
        {
            return await _context.SecondLevelCategories
                .Include(s => s.FirstLevelCategory)
                .ToListAsync() ?? Enumerable.Empty<SecondLevelCategory>();
        }

        public async Task<SecondLevelCategory?> GetSecondLevelCategoryByIdAsync(int id)
        {
            return await _context.SecondLevelCategories
                .Include(s => s.FirstLevelCategory)
                .FirstOrDefaultAsync(s => s.Id == id);
        }

        public async Task<IEnumerable<SecondLevelCategory>> GetSecondLevelCategoriesByFirstLevelCategoryIdAsync(int firstLevelCategoryId)
        {
            return await _context.SecondLevelCategories
                .Include(s => s.FirstLevelCategory)
                .Where(s => s.FirstLevelCategoryId == firstLevelCategoryId)
                .ToListAsync() ?? Enumerable.Empty<SecondLevelCategory>();
        }

        public async Task<SecondLevelCategory> CreateSecondLevelCategoryAsync(SecondLevelCategory secondLevelCategory)
        {
            // 验证一级分类是否存在
            var firstLevelCategoryExists = await _context.FirstLevelCategories
                .AnyAsync(f => f.Id == secondLevelCategory.FirstLevelCategoryId);
            if (!firstLevelCategoryExists)
            {
                throw new ArgumentException("Invalid first level category id");
            }

            // 验证分类编号是否唯一
            if (await _context.SecondLevelCategories.AnyAsync(s => s.CategoryCode == secondLevelCategory.CategoryCode))
            {
                throw new ArgumentException("Category code already exists");
            }

            // 可以在这里添加其他业务逻辑

            _context.SecondLevelCategories.Add(secondLevelCategory);
            await _context.SaveChangesAsync();
            return secondLevelCategory;
        }

        public async Task<bool> UpdateSecondLevelCategoryAsync(SecondLevelCategory secondLevelCategory)
        {
            // 检查二级分类是否存在
            var existingCategory = await _context.SecondLevelCategories.FindAsync(secondLevelCategory.Id);
            if (existingCategory == null)
            {
                return false;
            }

            // 验证一级分类是否存在
            var firstLevelCategoryExists = await _context.FirstLevelCategories
                .AnyAsync(f => f.Id == secondLevelCategory.FirstLevelCategoryId);
            if (!firstLevelCategoryExists)
            {
                return false;
            }

            // 更新属性
            existingCategory.FirstLevelCategoryId = secondLevelCategory.FirstLevelCategoryId;
            existingCategory.CategoryName = secondLevelCategory.CategoryName;
            existingCategory.MaterialDescription = secondLevelCategory.MaterialDescription;
            existingCategory.Unit = secondLevelCategory.Unit;
            existingCategory.SafetyStock = secondLevelCategory.SafetyStock;
            existingCategory.Status = secondLevelCategory.Status;
            existingCategory.CategoryCode = secondLevelCategory.CategoryCode;

            // 可以在这里添加其他业务逻辑

            _context.Entry(existingCategory).State = EntityState.Modified;
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<bool> DeleteSecondLevelCategoryAsync(int id)
        {
            var secondLevelCategory = await _context.SecondLevelCategories.FindAsync(id);
            if (secondLevelCategory == null)
            {
                return false;
            }

            // 可以在这里添加业务逻辑，例如检查是否有相关的物料

            _context.SecondLevelCategories.Remove(secondLevelCategory);
            await _context.SaveChangesAsync();
            return true;
        }
    }
}